
import { Subject, Observable } from 'rxjs';
export type RxWebSocketOptions = {
  url: string, // 连接地址
  reConnectTime?:number // 连接失败重连间隔时间 单位毫秒
}
export class RxWebSocket2 {
  private url: string = '';
  private ws: WebSocket|undefined;
  public wsOpenSubject: Subject<any>| null = null;
  private reconnectTimer: NodeJS.Timeout| null = null;
  private reconnectTime: number = 2000; // 连接失败默认间隔2秒重连
  private heartBeatTimer: NodeJS.Timeout| null = null;
  private heartBeatTime: number = 300000;
  private socket =  new Subject();

  constructor(opt: RxWebSocketOptions) {
    this.url = opt.url;
    this.reconnectTime = opt.reConnectTime || this.reconnectTime;
    this.createSocket();
  }

  public createSocket() {
    this.ws = new WebSocket(this.url)
    this.ws.onopen = () => {
      
    }
    this.ws.onmessage = (message: MessageEvent) => {

    }
    this.ws.onerror = () => {

    }
    this.ws.onclose = () => {

    }
  }
  
  
}

// 使用命名空间创建单列模式调用
export namespace RxWebSocketSingleton {
  let instance: RxWebSocket2;
  export function getInstance(opt: RxWebSocketOptions): RxWebSocket2 {
      if (!instance) {
          instance = new RxWebSocket2(opt);
      }
      return instance;
  }
}